package com.cxd.performance.listener;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.excel.write.style.column.LongestMatchColumnWidthStyleStrategy;
import com.cxd.performance.domain.MessageDomain;
import com.cxd.performance.domain.PerformanceAmountBaseInfo;
import com.cxd.performance.exception.BusinessException;
import com.cxd.performance.service.MailSenderService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;

import java.io.File;
import java.util.*;

@Component
@Slf4j
public class PerformanceAmountAnalysisEventListener extends AnalysisEventListener<PerformanceAmountBaseInfo> {

    List<PerformanceAmountBaseInfo> performanceAmountBaseInfoList = new ArrayList<>();

    private static final Map<String, String> mailMap;

    private final MailSenderService mailSenderService;

    static {
        mailMap = new HashMap<>();
        mailMap.put("陈小东","1765547530@qq.com");
        mailMap.put("钟水竞","269122713@qq.com");
        mailMap.put("吴思儒","sawyawoo@qq.com");
        mailMap.put("黄祖燕1","1431785687@qq.com");
        mailMap.put("陈惠嫦","841732588@qq.com");
        mailMap.put("黄志豪","13535515227@163.com");
        mailMap.put("陈俊杰","934028282@qq.com");
        mailMap.put("彭有华","875363958@qq.com");
        mailMap.put("黄海霞","280298418@qq.com");
        mailMap.put("梁丽华1","996439903@qq.com");
        mailMap.put("汪凯","1743078080@qq.com");
        mailMap.put("朱龙平","zlp17379721018@163.com");
        mailMap.put("何宇杭","799560793@qq.com");
        mailMap.put("李军","254135394@qq.com");
        mailMap.put("张志琪","1442606207@qq.com");
        mailMap.put("钟颖","785632292@qq.com");
        mailMap.put("曹莎","729833304@qq.com");
        mailMap.put("林夏豪","939187120@qq.com");
        mailMap.put("王素芳","707595299@qq.com");
        mailMap.put("王勤","645800823@qq.com");
        mailMap.put("黄进广","334238038@qq.com");
        mailMap.put("袁梦1","YM_jobmail@163.com");
        mailMap.put("黄锦辉","395853639@qq.com");
        mailMap.put("梁国聪","810478056@qq.com");
        mailMap.put("陆超","295205757@qq.com");
        mailMap.put("李芯妍","360067592@qq.com");
        mailMap.put("王国彬","491734009@qq.com");
        mailMap.put("胡绍波1","932972523@qq.com");
        mailMap.put("黄柏青","806242204@qq.com");
        mailMap.put("张立飞","541822547@qq.com");
        mailMap.put("张晓君","13723668868@139.com");
        mailMap.put("石元超","452322405@qq.comm");
        mailMap.put("吴翼佳","277054555@qq.com");
        mailMap.put("刘荣生","ale1628906564@163.com");
        mailMap.put("莫云烽","492262542@qq.com");
        mailMap.put("韦冠","497135183@qq.com");
        mailMap.put("马海恩","420300764@qq.com");
        mailMap.put("刘少龙","liushaolongg@163.com");
        mailMap.put("龚宇","13697099832@163.com");
        mailMap.put("许国青","1026366563@qq.com");
        mailMap.put("林奕翠","512444548@qq.com");
        mailMap.put("邱宁凯","qiuningkai@126.com");
        mailMap.put("许凯东","863516112@qq.com");
        mailMap.put("朱元凯","zhuyuankai_01@163.com");
        mailMap.put("胡源源","879754072@qq.com");
        mailMap.put("赖应德","525443088@qq.com");
        mailMap.put("沈聪","shencong552@qq.com");
        mailMap.put("刘鹏1","liupeng3231@163.com");
        mailMap.put("亓文娜","1821609526@qq.com");
        mailMap.put("胡睿","544458163@qq.com");
        mailMap.put("余毅","247075299@qq.com");
        mailMap.put("蔡长松","534758557@qq.com");
        mailMap.put("黄翠霞","mila_huang001@163.com");
        mailMap.put("唐美","3108162157@qq.com");
        mailMap.put("邹燕","15986631059@163.com");
        mailMap.put("徐湘峰","1315013417@qq.com");
        mailMap.put("唐翔","133144954@qq.com");
        mailMap.put("黄宏光","17097082364@163.com");
        mailMap.put("胡明深","690635617@qq.com");
        mailMap.put("李志1","76197866@qq.com");
        mailMap.put("周展宏","hong_286@163.com");
        mailMap.put("杨国新","349323198@qq.com");
        mailMap.put("王开1","872464959@qq.com");
        mailMap.put("姜英楠","183592464@qq.com");
        mailMap.put("王玲利","1652795512@qq.com");
        mailMap.put("朱鑫福","949776130@qq.com");
        mailMap.put("陈梦","15924185940@163.com");
        mailMap.put("吴文汀","422242620@qq.com");
        mailMap.put("陈政","506638174@qq.com");
        mailMap.put("张荣勋","3204068880@qq.com");
        mailMap.put("赵明月","2692342812@qq.com");
        mailMap.put("刘中旭","710887878@qq.com");
        mailMap.put("何成宇","hcy_email@163.com");
        mailMap.put("张肖玉","zxy12586@163.com");
        mailMap.put("李尚积","xiu712@qq.com");
        mailMap.put("伍震彬","348639192@qq.com");
        mailMap.put("张君盛","zjstccc@163.com");
        mailMap.put("曾浩明","329535006@qq.com");
        mailMap.put("张海皓","moonlight_sea@163.com");
        mailMap.put("丁佳辉","15623221261@163.com");
        mailMap.put("邓忠贵","1942648785@qq.com");
        mailMap.put("唐上锟","379639511@qq.com");
        mailMap.put("贺宇涛","heyutao_666@163.com");
    }

    public PerformanceAmountAnalysisEventListener(MailSenderService mailSenderService) {
        this.mailSenderService = mailSenderService;
    }

    @Override
    public void invoke(PerformanceAmountBaseInfo performanceAmountBaseInfo, AnalysisContext analysisContext) {
        performanceAmountBaseInfoList.add(performanceAmountBaseInfo);
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        performanceAmountBaseInfoList.remove(0);
        for (PerformanceAmountBaseInfo performanceAmountBaseInfo : performanceAmountBaseInfoList) {
            String employeeName = performanceAmountBaseInfo.getEmployeeName();
            Calendar calendar = Calendar.getInstance();
            int year = calendar.get(Calendar.YEAR);
            int month = calendar.get(Calendar.MONTH);
            String path = "F:\\Elead\\美创一组绩效\\绩效汇总\\";
            isPathExist(path);
            String targetFilePath = path + "美创第一项目组" + "-" + year + "年" + month + "月-" +employeeName+"-"+ "月度绩效.xlsx";
            EasyExcel.write(targetFilePath, PerformanceAmountBaseInfo.class).sheet(month + "月绩效")
                    .registerWriteHandler(new LongestMatchColumnWidthStyleStrategy())
                    .doWrite(Collections.singletonList(performanceAmountBaseInfo));
            try{
                //发送邮件：
                String mail = null;
                if (StringUtils.isNotBlank(employeeName) && !ObjectUtils.isEmpty(mailMap)) {
                    mail = mailMap.get(employeeName);
                    if (StringUtils.isBlank(mail)) {
                        log.error("员工：" + employeeName + ",邮箱地址为空!");
                        throw new BusinessException("20001", "员工：" + employeeName + ",邮箱地址为空!");
                    }
                }
                if (StringUtils.isBlank(mail)) {
                    log.error("员工：" + employeeName + ",邮箱地址为空!");
                    throw new BusinessException("20001", "员工：" + employeeName + ",邮箱地址为空!");
                }
                if (!checkMail(mail)) {
                    log.error("员工：" + employeeName + "邮件格式错误,邮箱为：{}", mail);
                    throw new BusinessException("20002", "员工：" + employeeName + "邮件格式错误,邮箱为：{}" + mail);
                }

                MessageDomain messageDomain = new MessageDomain();
                messageDomain.setTo(new String[]{mail});
                messageDomain.setSubject("美创第一项目组-" + performanceAmountBaseInfo.getEmployeeName() + "-" + (year) + "年" + month + "月-项目绩效");
                String content = "<html lang=\"en\">\n" +
                        "<head>\n" +
                        "    <meta charset=\"UTF-8\">\n" +
                        "    <meta ame=\"viewport\" cotet=\"width=device-width, iitial-scale=1.0\">\n" +
                        "    <title>Documet</title>\n" +
                        "</head>\n" +
                        "<body>\n" +
                        "    <div style=\"font-size:20px\">&emsp;&emsp;如果您对本月项目绩效有任何疑义,请您在收到邮件通知起3个工作日内发送邮件向PM进行申诉。</div>\n" +
                        "    <div style=\"font-size:20px\">若在申诉期间内未提出疑义，视为您认可本次绩效公布结果，公司后续不再接受申诉，绩效结果以本次通知为准。</div><br>\n" +
                        "    <div style='color: red;font-size:20px'>评分明细请看附件：</div><br><br>\n" +
                        "    <div style=\"font-size:20px\">申诉对接人：</div>\n" +
                        "\t<div style=\"font-size:20px\">美创第一项目组PM:陈小东(邮箱：chexiaodog@e-lead.cn)</div>\n" +
                        "</body>\n" +
                        "</html>";
                messageDomain.setContent(content);
                messageDomain.setFilePath(targetFilePath);
                mailSenderService.sendAttachmentsMail(messageDomain);

            }catch (Exception e) {
                log.error("发送项目绩效金额失败", e);
            }
        }
    }

    private void isPathExist(String filepath) {
        File file = new File(filepath);
        if (!file.exists()) {
            log.info("dir not exists, create it ...");
            file.mkdir();
        }
    }

    private boolean checkMail(String mail) {
        if(mail.contains("@")&&mail.contains(".")) {
            if(mail.lastIndexOf(".")>mail.lastIndexOf("@")) {
                return true;
            }
        }else {
            return false;
        }
        return true;
    }
}
